package vn.com.bookstore.activity;

import java.util.ArrayList;

import vn.com.bookstore.BookStoreApplication;
import vn.com.bookstore.Config;
import vn.com.bookstore.ConnectActivity;
import vn.com.bookstore.ObjectFragment;
import vn.com.bookstore.R;
import vn.com.bookstore.adapter.CoverAdapter;
import vn.com.bookstore.adapter.MyPagerAdapter;
import vn.com.bookstore.fragment.ListBookFragment;
import vn.com.bookstore.fragment.StoreFragment.OnStoreFragmentListener;
import vn.com.bookstore.model.Book;
import vn.com.bookstore.model.ListBook;
import vn.com.bookstore.model.Store;
import vn.com.bookstore.network.NetworkAdapter;
import vn.com.bookstore.network.NetworkAdapter.OnNetworkListener;
import vn.com.bookstore.util.transformer.DepthPageTransformer;
import vn.com.bookstore.util.transformer.DepthPageTransformerWithAlpa;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class StoreActivity extends ConnectActivity implements OnNetworkListener, OnStoreFragmentListener, OnItemClickListener {
	public static final String TAG = "StoreActivity";

	private ViewPager pager;
	TextView titleStrip;
	protected Gallery mCoverFlow;

	MyPagerAdapter mContainAdapter;
	protected CoverAdapter mCoverAdapter;

	ArrayList<Book> mData;

	Store selectedStore;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_store);
		setTitle(this.getString(R.string.activity_store_title), this.getString(R.string.activity_store_title_desc), true);
		findViewById();

		mContainAdapter = new MyPagerAdapter(this, getSupportFragmentManager());
		pager.setAdapter(mContainAdapter);

		pager.setCurrentItem(1);

		mCoverAdapter = new CoverAdapter(this, mData);
		mCoverFlow.setAdapter(mCoverAdapter);

		mCoverFlow.setOnItemClickListener(this);

		mCoverFlow.setSelection(mCoverAdapter.getCount() / 2);

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if(BookStoreApplication.isTablet(mApplication)) return;
				switch (arg0) {
				case 0:
					titleStrip.setText(getString(R.string.title_store));
					break;
				case 1:
					if (selectedStore == null)
						titleStrip.setText(getString(R.string.title_book));
					else
						titleStrip.setText(selectedStore.getName());
					break;
				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		getBanner(false);

		if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && !BookStoreApplication.isTablet(this)) {
			pager.setPageTransformer(true, new DepthPageTransformerWithAlpa());
		}

		if (!BookStoreApplication.isTablet(this)) {
			boolean firstRun = mApplication.getValue(BookStoreApplication.KEY_FIRST_RUN, true);
			if (firstRun) {
				startActivity(new Intent(this, TutorialDialog.class));
			}
		}

	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		if (intent.hasExtra("message")) {
			String message = intent.getStringExtra("message");
			if (message.equals("close tut")) {
				pager.setCurrentItem(0, true);
			}
		}
	}

	public void getBanner(final boolean forceLoad) {
		// get list of Banner
		new Thread() {
			public void run() {
				NetworkAdapter net = new NetworkAdapter(StoreActivity.this, mHandler, StoreActivity.this);
				Config.NetObject obj = Config.BookStore.getUrlListBanner(0, 1, Config.LIMIT);
				net.sendHttpGet(ConnectActivity.METHOD_GET_BOOK, obj.url, obj.param, forceLoad);
			};
		}.start();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void findViewById() {
		super.findViewById();
		pager = (ViewPager) findViewById(R.id.pager);
		titleStrip = (TextView) findViewById(R.id.title);
		mCoverFlow = (Gallery) findViewById(R.id.cover_store);
	}

	@Override
	public void onResponse(int idMethod, boolean isSuccess, String json) {
		if (isSuccess) {
			switch (idMethod) {
			case ConnectActivity.METHOD_GET_BOOK:
				mData = new ArrayList<Book>(ListBook.parseListGson(json));
				mCoverAdapter.setData(mData);
				break;
			default:
				break;
			}

		}
	}

	@Override
	public void onStoreClicked(AdapterView<?> adapter, View itemStore, int position, Store store) {
		ListBookFragment bookFragment = (ListBookFragment) this.mContainAdapter.getItem(1);
		bookFragment.setData(store, false);
		selectedStore = store;
		pager.setCurrentItem(1, true);
		titleStrip.setText(store.getName());
	}

	private static long back_pressed;

	@Override
	public void onBackPressed() {
		if (back_pressed + 2000 > System.currentTimeMillis())
			super.onBackPressed();
		else
			Toast.makeText(getBaseContext(), getString(R.string.back_again), Toast.LENGTH_SHORT).show();
		back_pressed = System.currentTimeMillis();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

		try {
			if (mData == null || mData.isEmpty())
				return;
			int index = position % mData.size();
			Book book = mData.get(index);
			Intent i = new Intent(this, ChapterActivity.class);
			i.putExtra(Book.TAG, book);
			startActivity(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_home, menu);
		return true;
	}

	public void mnClicked(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.help:
			startActivity(new Intent(this, HelpActivity.class));
			break;
		case R.id.refresh:
			getBanner(true);
			((ObjectFragment) mContainAdapter.getItem(0)).loadData(true);
			((ObjectFragment) mContainAdapter.getItem(1)).loadData(true);
			break;
		default:
			break;
		}

	}
}
